package com.cxm.qyyz.core.http;


import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableSource;
import io.reactivex.rxjava3.core.ObservableTransformer;
import io.reactivex.rxjava3.functions.Function;

public class RxUtil {

    public static <T> ObservableTransformer<BaseResponse<T>, T> transformer() {
        return new ObservableTransformer<BaseResponse<T>, T>() {
            @Override
            public ObservableSource<T> apply(Observable<BaseResponse<T>> upstream) {
                return upstream.map(new Function<BaseResponse<T>, T>() {
                    @Override
                    public T apply(BaseResponse<T> tBaseResponse) throws Exception {
                        if (tBaseResponse.getCode() == 0) {
                            return tBaseResponse.getData();
                        }
//                        return Observable.just(1).delay(1000, TimeUnit.MILLISECONDS);
                        throw new ApiException(tBaseResponse.getCode(), tBaseResponse.getMessage());
                    }
                });
            }
        };
    }
}
